/*

 */
package com.insigma.business.qrcode.common.util;


import com.insigma.business.qrcode.common.vo.RestfulResult;
import org.apache.commons.lang3.StringUtils;


public class RestfulResultUtil {

    private RestfulResultUtil(){}

    
    @SuppressWarnings("unchecked")
    public static <T> RestfulResult<T> buildSuccess(final T t){
        final RestfulResult restfulResult = new RestfulResult();
        restfulResult.setCode(RestfulCodeEnum.SUCCESS.getCode());
        restfulResult.setMsg(RestfulCodeEnum.SUCCESS.getDesc());
        restfulResult.setData(t);

        return restfulResult;
    }

    
    public static RestfulResult buildSuccess(){
        final RestfulResult restfulResult = new RestfulResult();
        restfulResult.setCode(RestfulCodeEnum.SUCCESS.getCode());
        restfulResult.setMsg(RestfulCodeEnum.SUCCESS.getDesc());

        return restfulResult;
    }

    
    @SuppressWarnings("unchecked")
    public static <T> RestfulResult<T> setupFailed(final RestfulResult restfulResult,
                                                   final String errorCode,
                                                   final String customErrorMsg) {
        restfulResult.setCode(errorCode);
        restfulResult.setMsg(customErrorMsg);

        return restfulResult;
    }

    
    @SuppressWarnings("unchecked")
    public static <T> RestfulResult<T> buildFailed(final RestfulCodeEnum resultCodeEnum,final String customErrorMsg){
        final RestfulResult restfulResult = new RestfulResult();
        restfulResult.setCode(resultCodeEnum.getCode());
        restfulResult.setMsg(StringUtils.isBlank(customErrorMsg)?resultCodeEnum.getDesc():customErrorMsg);

        return restfulResult;
    }

}
